package com.zxc.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.zxc.common.to.mq.SeckillOrderTo;
import com.zxc.gulimall.order.entity.OrderEntity;
import com.zxc.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Description: 定时关闭秒杀订单
 */

@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Service
@Component
public class OrderSeckillListener {

    @Autowired
    private OrderService orderService;


    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        try {
            //创建秒杀单的详细信息
            log.info("接收到秒杀订单创建的消息，创建秒杀单的详细信息，正在创建中...");
            orderService.createSkillOrder(seckillOrderTo);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

}
